// 'user strict';
'use strict';
const Controller = require('egg').Controller;
class JspangController extends Controller {

  /* new */
  // ----------------------------------

  async add() {
    const ctx = this.ctx
    ctx.cookies.set("user", "jspang.com")
    ctx.body = {
      status: 200,
      data: 'Cookie添加成功'
    }
  }
  async del() {
    const ctx = this.ctx
    ctx.cookies.set("user", null)
    ctx.body = {
      status: 200,
      data: 'Cookie删除成功'
    }
  }
  async editor() {
    const ctx = this.ctx
    ctx.cookies.set("user", 'bilibili')
    ctx.body = {
      status: 200,
      data: 'Cookie修改成功'
    }
  }
  async show() {
    const ctx = this.ctx
    const user = ctx.cookies.get("user")
    console.log(user)
    ctx.body = {
      status: 200,
      data: 'Cookie显示成功'
    }
  }
  /* to use egg-view-ejs to show/render the html  */
  async index() {
    // there ctx is assigned by this
    const { ctx } = this;
    await ctx.render(
      'jspang.html',
      {
        id: 2021,
        name: "homg",
        age: 18,
        skill: "massage"
      });
    /* old */
    // await ctx.render('jspang.html');

  }
  /* post request mode */
  async addPost() {
    const ctx = this.ctx;

    ctx.body = {
      status: 200,
      data: ctx.request.body
    }
    // ctx.body = "add page";
  }

  /* set for strict parameter pass mode */
  async getGirl2() {
    const ctx = this.ctx;
    ctx.body = 'getGirl2:' + "name:" + ctx.params.name + "," + "age:" + ctx.params.age
  }

  // set for :free to pass parameters:
  async getGirl() {
    const ctx = this.ctx;
    const res = await ctx.service.jspang.getGirl("1111")
    ctx.body = res
    // ctx.body = ctx.query
  }

  /* old */
  async indexPage() {
    const { ctx } = this;

    /* new: */
    await ctx.render(
      'jspang.html',
      {
        id: 2021,
        name: "homg",
        age: 18,
        skills: [
          '泰式按摩',
          '精油搓背',
          '水疗SPA'
        ],
        skill: "massage"
      });

    /* old */
    /* try unit test: */
    // ctx.body = 'hi,egg'
    // ctx.body = '<h1> I am Jspang</h1 >';


  }
  async getGirls() {
    const { ctx } = this;
    await new Promise(
      resolve => {
        setTimeout(() => {
          resolve(ctx.body = '<h1>a is coming to you</h1>');
        }, 1000);
        console.log(ctx.body);
      }
    );
  }

}
module.exports = JspangController;
